package ltd.rxjavademo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;

import bean.CheckVerData;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit.HttpMethods;
import retrofit.ProgressSubscriber;
import retrofit.SubscriberOnNextListener;
import rx.Subscriber;

public class MainActivity extends AppCompatActivity {
    @Bind(R.id.click_me_BN)
    Button clickMeBN;
    @Bind(R.id.result_TV)
    TextView resultTV;

    Subscriber<String> subscriber ;
    private SubscriberOnNextListener getMsgOnNext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        getMsgOnNext = new SubscriberOnNextListener<CheckVerData<String>>() {
            @Override
            public void onNext(CheckVerData<String> data) {
                resultTV.setText(data.getVersion() + data.getData());
            }
        };
    }

    @OnClick(R.id.click_me_BN)
    public void onClick() {
//        getMovieFromOB();
        getMsg();
    }

    //进行网络请求
//    private void getMovie(){
//        RequestSerives requestSerives = RetrofitWrapper.getInstance().create(RequestSerives.class);//这里采用的是Java的动态代理模式
//        Call<MSGResult> call = requestSerives.getMsgResultCall("/serverapi/checkver/bootver",691,0);//传入我们请求的键值对的值
//        call.enqueue(new Callback<MSGResult>() {
//            @Override
//            public void onResponse(Call<MSGResult> call, Response<MSGResult> response) {
//                resultTV.setText(response.body().getData());
//                Log.e("成功",response.body().toString());
//            }
//
//            @Override
//            public void onFailure(Call<MSGResult> call, Throwable t) {
//                resultTV.setText(t.getMessage());
//                Log.e("失败","失败");
//            }
//        });
//    }
//    //进行网络请求
//    private void getMovieFromOB(){
//        RequestSerives requestSerives = RetrofitWrapper.getInstance().create(RequestSerives.class);//这里采用的是Java的动态代理模式
//        requestSerives.getCheckVer("/serverapi/checkver/bootver",691,0)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Subscriber<MSGResult>() {
//                    @Override
//                    public void onCompleted() {
//                        Toast.makeText(MainActivity.this, "Get Msg Completed", Toast.LENGTH_SHORT).show();
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        resultTV.setText(e.getMessage());
//                    }
//
//                    @Override
//                    public void onNext(MSGResult msgResult) {
////                        resultTV.setText(msgResult.getData());
//                    }
//                });
//    }
//    private void getMsg(){
//        subscriber = new Subscriber<String>() {
//            @Override
//            public void onCompleted() {
//                Toast.makeText(MainActivity.this, "Get Top Movie Completed", Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                resultTV.setText("错误：" + e.getMessage());
//            }
//
//            @Override
//            public void onNext(String data) {
//                resultTV.setText(data);
//            }
//
//        };
//        HttpMethods.getInstance().getMsg(subscriber, "/serverapi/checkver/bootver",691, 2);
//    }

    private void getMsg(){
        HttpMethods.getInstance().getMsg(new ProgressSubscriber<String>(getMsgOnNext,MainActivity.this), "/serverapi/checkver/bootver",691, 0);
    }
}
